window.onload=function(){
	$(".installinfos").css("backgroundColor","#224247").siblings().css("backgroundColor","#365d62");
	//查询
	$(".queryBoxBtn").click(function(){
		var writeContent = $(".installQueryBox").val();
		if(writeContent=="%"){
			hint("非法输入，请正确输入编号！");
		}else{
			window.location.href='/admin/device/installInfos?condition='+writeContent;
		}
	});
	//添加
	$(".addInstallInfosBtn").click(function(){
		$(".addIDeviceCode").val()=="";
		$("#addIlabelBox").html("");
		var _this=$(this);
		layui.use('layer', function(){
		  	var layer = layui.layer;
		  	layer.open({
				type: 1, 
				title: ['添加安装信息', 'font-size:18px;'],
				area: ['800px', '500px'],
			  	content: $(".addInstallInfos"),
			}); 
		});
		DropBoxFn(".addInstallStatus",".addsDropBox");//安装状态下拉框
		installFn(".addInstallMen",".addInstallMenList",".addInstallMenTel");//安装人下拉框
		dealerFn(".addInstallDealer",".addDealerList");//经销商下拉框
		//点击取消按钮
		$(".addINoBtn").unbind('click').click(function(){
			window.location.reload();
		});
		//点击确定
		$(".addIOkBtn").unbind('click').click(function(){
			if($(".addInstallUserName").val()==""){
				$("#addIlabelBox").html("姓名不能为空。");
			}else if($(".addInstallUserPhone").val()==""){
				$("#addIlabelBox").html("用户电话不能为空。");
			}else if($(".addInstallAddress").val()==""){
				$("#addIlabelBox").html("安装地址不能为空。");
			}else if($(".addIDeviceCode").val()==""){
				$("#addIlabelBox").html("设备编号不能为空。");
			}else if($(".addInstallStatus").val()==""){
				$("#addIlabelBox").html("安装状态不能为空。");
			}else if($(".addInstallDealer").val()==""){
				$("#addIlabelBox").html("请输入经销商。");
			}else if($(".addInstallMen").val()==""){
				$("#addIlabelBox").html("请输入安装人姓名。");
			}else if($(".addInstallMenTel").val()==""){
				$("#addIlabelBox").html("请输入安装人手机号码。");
			}else if(!(/^1[34578]\d{9}$/.test($(".addInstallUserPhone").val()))||!(/^1[34578]\d{9}$/.test($(".addInstallMenTel").val()))){
				$("#addIlabelBox").html("手机号码输入有误，请重新输入！");
			}else if(!(/^[0-9]{16}$/.test($(".addIDeviceCode").val()))){
				$("#addIlabelBox").html("输入有误，请输入16位设备编号！");
			}else if($("#addInstallInfosDate").val()==""){
				$("#addIlabelBox").html("请添加安装日期！");
			}
			else{
				$("#addIlabelBox").html("");
				$.ajax({
					url:"/admin/api/device/addInstallInfo",
					type:"post",
					dataType:"json",
					data:{
						device_code:$(".addIDeviceCode").val(),
			            customer_name:$(".addInstallUserName").val(),
			            phone:$(".addInstallUserPhone").val(),
			            installation_address:$(".addInstallAddress").val(),
			            operator:$(".addInstallMen").val(),
			            operator_phone:$(".addInstallMenTel").val(),
			            install_at:$("#addInstallInfosDate").val(),
			            status:$(".addInstallStatus").val(),
			            dealer:$(".addInstallDealer").val(),
			            remark:$(".other_addText").val()
					},
					success:function(data){
						if(data.status.errCode==0){
							_this.parent().parent().siblings(".DeviceCodeCell").html($(".addIDeviceCode").val());
							_this.parent().parent().siblings(".UserNameCell").html($(".addInstallUserName").val());
							_this.parent().parent().siblings(".UserPhoneCell").html($(".addInstallUserPhone").val());
							_this.parent().parent().siblings(".AddressCell").html($(".addInstallAddress").val());
							_this.parent().parent().siblings(".MenCell").html($(".addInstallMen").val());
							_this.parent().parent().siblings(".MenTelCell").html($(".addInstallMenTel").val());
							_this.parent().parent().siblings(".DealerCell").html($(".addInstallDealer").val());
							_this.parent().parent().siblings(".other_editTextCell").html($(".other_addText").val());
							_this.parent().parent().siblings(".YMD").html($("#addInstallInfosDate").val());
							_this.parent().parent().siblings(".statusCell").html($(".addInstallStatus").val());
							window.location.href="/admin/device/installInfos";
						}else{
							$("#addIlabelBox").html(data.status.message);
						}
					}
				})
			}
		});

	})
	//编辑
	$(".writeIBtn").click(function(){
		$("#editIlabelBox").html("");
		var _this=$(this);
		//设置可移动弹框
		layui.use('layer', function(){
		  	var layer = layui.layer;
		  	layer.open({
				type: 1, 
				title: ['编辑安装信息', 'font-size:18px;'],
				area: ['800px', '500px'],
			  	content: $(".editInstallInfos"),
			}); 
		});
		//获取编辑信息
		$(".editIUserName").val(_this.parent().parent().siblings(".UserNameCell").html());
		$(".editIUserPhone").val(_this.parent().parent().siblings(".UserPhoneCell").html());
		$(".editIAddress").val(_this.parent().parent().siblings(".AddressCell").html());
		$(".editIDeviceCode").val(_this.parent().parent().siblings(".DeviceCodeCell").html());
		$(".sBox").val(_this.parent().parent().siblings(".statusCell").html());
		$(".editIDealer").val(_this.parent().parent().siblings(".DealerCell").html());
		$(".editIMen").val(_this.parent().parent().siblings(".MenCell").html());
		$(".editIMenTel").val(_this.parent().parent().siblings(".MenTelCell").html());
		$("#editInstallInfosDate").val(_this.parent().parent().siblings(".YMD").html());
		$(".other_editText").val(_this.parent().parent().siblings(".other_editTextCell").html());
		DropBoxFn(".sBox",".sDropBox");//安装状态下拉框		
		installFn(".editIMen",".installMenList",".editIMenTel");//安装人下拉框
		dealerFn(".editIDealer",".editDealerList");//经销商下拉框
		//点击取消按钮
		$(".editINoBtn").unbind('click').click(function(){
			window.location.reload();
		});
		//确定按钮
		$(".editIOkBtn").unbind('click').click(function(){
			if($(".editIUserName").val()==""){
				$("#editIlabelBox").html("用户姓名不能为空。");
			}else if($(".editIUserPhone").val()==""){
				$("#editIlabelBox").html("用户手机号码不能为空。");
			}else if($(".editIAddress").val()==""){
				$("#editIlabelBox").html("安装地址不能为空。");
			}else if($(".sBox").val()==""){
				$("#editIlabelBox").html("安装状态不能为空。");
			}else if($(".editIDealer").val()==""){
				$("#editIlabelBox").html("经销商不能为空。");
			}else if($(".editIMen").val()==""){
				$("#editIlabelBox").html("安装人不能为空。");
			}else if($(".editIMenTel").val()==""){
				$("#editIlabelBox").html("安装人手机号码不能为空。");
			}else if($("#editIDeviceCode").val()==""){
				$("#editIlabelBox").html("设备编号不能为空。");
			}else if(!(/^1[34578]\d{9}$/.test($(".editIUserPhone").val()))||!(/^1[34578]\d{9}$/.test($(".editIMenTel").val()))){
				$("#editIlabelBox").html("手机号码输入有误，请重新输入！");
			}else if(!(/^[0-9]{16}$/.test($(".editIDeviceCode").val()))){
				$("#editIlabelBox").html("输入有误，请输入16位设备编号！");
			}else if($("#editInstallInfosDate").val()==""){
				$("#editIlabelBox").html("安装日期不能为空！");
			}else{
				$("#editIlabelBox").html("");
				$.ajax({
					url:"/admin/api/device/updateInstallInfo",
					type:"post",
					dataType:"json",
					data:{
						id:_this.siblings("input").val(),
						device_code:$(".editIDeviceCode").val(),
			            customer_name:$(".editIUserName").val(),
			            phone:$(".editIUserPhone").val(),
			            installation_address:$(".editIAddress").val(),
			            operator:$(".editIMen").val(),
			            operator_phone:$(".editIMenTel").val(),
			            install_at:$("#editInstallInfosDate").val(),
			            status:$(".sBox").val(),
			            dealer:$(".editIDealer").val(),
			            remark:$(".other_editText").val()
					},
					success:function(data){
						if(data.status.errCode==0){
							_this.parent().parent().siblings(".DeviceCodeCell").html($(".editIDeviceCode").val());
							_this.parent().parent().siblings(".UserNameCell").html($(".editIUserName").val());
							_this.parent().parent().siblings(".UserPhoneCell").html($(".editIUserPhone").val());
							_this.parent().parent().siblings(".AddressCell").html($(".editIAddress").val());
							_this.parent().parent().siblings(".MenCell").html($(".editIMen").val());
							_this.parent().parent().siblings(".MenTelCell").html($(".editIMenTel").val());
							_this.parent().parent().siblings(".DealerCell").html($(".editIDealer").val());
							_this.parent().parent().siblings(".other_editTextCell").html($(".other_editText").val());
							_this.parent().parent().siblings(".YMD").html($("#editInstallInfosDate").val());
							_this.parent().parent().siblings(".statusCell").html($(".sBox").val());
							$("#editIlabelBox").html("编辑成功");
						}else{
							$("#editIlabelBox").html(data.status.message);
						}
					}
				})
			}
			
		});
	});
	//删除
	$(".deleteIBtn").click(function(){
		$("#deleteIlabelBox").html("");
		var _this=$(this);
		layui.use('layer', function(){
		  	var layer = layui.layer;
		  	layer.open({
				type: 1, 
				title: ['删除此条安装信息', 'font-size:18px;'],
				area: ['500px', '300px'],
			  	content: $(".deleteIInfos"),
			}); 
		});
		$(".deleteICancelBtn").unbind('click').click(function(){
			window.location.reload();
		});
		$(".deleteIOkBtn").unbind('click').click(function(){
			$.ajax({
				url:"/admin/api/device/deleteInstallInfo",
				type:"post",
				dataType:"json",
				data:{
					id:_this.next().val()
				},
				success:function(data){
						if(data.status.errCode==0){
							_this.parent().parent().parent().remove();
							$("#deleteIlabelBox").html("此条记录已成功删除");
						}else{
							$("#deleteIlabelBox").html(data.status.message);
						}
					}
			})
		});
	});
	//日期控件
	layui.use('laydate', function(){
	  	var element =laydate = layui.laydate
	})
	//经销商下拉框
	function dealerFn(objectBtn,objectList){
		$(objectBtn).unbind('click').click(function(event){
			event.stopPropagation();
			$(objectList).show();
			$.ajax({
				url:"/admin/api/device/dealers",
				type:"get",
				dataType:"json",
				success:function(data){
					if(data.status.errCode==0){
						var content = '';
						for(var i=0;i<data.data.length;i++){
							content = content+'<a class="layui-elip">'+data.data[i].dealer+'</a>';
						};
						$(objectList).html(content);
						$(objectList).children("a").unbind('click').click(function(){
							$(objectBtn).val($(this).html());
							$(objectList).hide();
						})
						$("body").unbind('click').click(function(){
							$(objectList).hide();
						});
					}else{
						hint(data.status.message);
					}
				}
			});	
		});
	}
	//安装人下拉框
	function installFn(oInstallMen,oInstallList,oTel){
		$(oInstallMen).unbind('click').click(function(event){
			event.stopPropagation();
			$(oInstallList).show();
			$.ajax({
				url:"/admin/api/device/installers",
				type:"get",
				dataType:"json",
				success:function(data){
					if(data.status.errCode==0){
						var content = '';
						for(var i=0;i<data.data.length;i++){
							content = content+'<a class="layui-elip"><span class="operator" style="display: inline;">'+data.data[i].operator+' </span> <span class="operator_phone">'+data.data[i].operator_phone+'</span></a>';
						};
						$(oInstallList).html(content);
						$(oInstallList).children("a").unbind('click').click(function(){
							$(oInstallMen).val($(this).children(".operator").html());
							$(oTel).val($(this).children(".operator_phone").html());
							$(oInstallList).hide();
						})
						$("body").unbind('click').click(function(){
							$(oInstallList).hide();
						});
					}else{
						hint(data.status.message);
					}
				},
			});	
		});
	}
}